import { StateMachine } from './StateMachine';
import { StateMachineConfig, StateConfig, StateTransition } from './type';

/**
 * 状态机构建器，提供链式API来创建状态机
 */
export class StateMachineBuilder {
    private config: StateMachineConfig;

    constructor(initialState: string) {
        this.config = {
            initialState,
            states: [],
            transitions: [],
            context: {},
        };
    }

    /**
     * 添加状态
     */
    addState(config: StateConfig): StateMachineBuilder {
        this.config.states.push(config);
        return this;
    }

    /**
     * 添加转换
     */
    addTransition(transition: StateTransition): StateMachineBuilder {
        this.config.transitions.push(transition);
        return this;
    }

    /**
     * 设置上下文
     */
    setContext(context: Record<string, any>): StateMachineBuilder {
        this.config.context = { ...this.config.context, ...context };
        return this;
    }

    /**
     * 构建状态机
     */
    build(): StateMachine {
        return new StateMachine(this.config);
    }
}
